var constant = require('../../core/common/constant');
dishService.$inject = ['$http'];

function dishService($http) {
    var context = constant.BaseUrl + '/sec/op/dishes/';

    function getDishes(callBack) {
        $http.get(context)
            .then(function successCallBack(response) {
                console.log(response);
                callBack(response);
            }, function errorCallBack(response) {
                callBack(response);
            });
    }

    function addDish(dish, callBack) {
        var fd = new FormData();
        console.log("data:" + dish);
        for (var key in dish)
            fd.append(key, dish[key]);
        $http.post(context, fd, {
            transformRequest: angular.indentity,
            headers: {'Content-Type': undefined}
        }).then(function successCallBack(resposne) {
            callBack(resposne);
        }, function errorCallBack(response) {
            callBack(response);
        });
    }

    function updateDish(dish, callBack) {
        var fd = new FormData();
        console.log("data:" + dish);
        for (var key in dish)
            fd.append(key, dish[key]);
        $http.post(context + "update", fd, {
            transformRequest: angular.indentity,
            headers: {'Content-Type': undefined}
        }).then(function successCallBack(response) {
            callBack(response);
        }, function errorCallBack(response) {
            callBack(response);
        })
    }

    function deleteDish(did, callBack) {
        $http.delete(context + did)
            .then(function successCallBack(response) {
                console.log(response);
                callBack(response);
            }, function errorCallBack(response) {
                callBack(response);
            });
    }

    function findByType(type, callBack) {
        $http.get(context + "byType/" + type)
            .then(function successCallBack(response) {
                console.log(response);
                callBack(response);
            }, function errorCallBack(response) {
                console.log(response);
                callBack(response);
            });
    }

    function findByBoth(type, name, callBack) {
        $http.get(context + "both/" + type + "/" + name)
            .then(function successCallBack(response) {
                console.log(response);
                callBack(response);
            }, function errorCallBack(response) {
                console.log(response);
                callBack(response);
            });
    }

    return {
        getDishes: getDishes,
        addDish: addDish,
        updateDish: updateDish,
        deleteDish: deleteDish,
        findByType: findByType,
        findByBoth: findByBoth
    }
}

module.exports = dishService;